package com.example.scenictourback.controller;

import com.example.scenictourback.common.Result;
import com.example.scenictourback.dto.TicketDTO;
import com.example.scenictourback.entity.Ticket;
import com.example.scenictourback.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class TicketController {

    @Autowired
    private TicketService ticketService;

    /**
     * 获取所有门票列表
     */
    @GetMapping("/ticket/all")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> listAllTickets() {
        List<TicketDTO> tickets = ticketService.listAllTickets();
        return Result.success(tickets);
    }

    /**
     * 根据景区ID获取门票列表 (原端点)
     */
    @GetMapping("/ticket/list")
    public Result<?> listTicketsByScenicAreaId(@RequestParam Integer scenicAreaId) {
        List<TicketDTO> tickets = ticketService.listTicketsByScenicAreaId(scenicAreaId);
        return Result.success(tickets);
    }
    
    /**
     * 根据景区ID获取门票列表 (新端点，匹配前端调用)
     */
    @GetMapping("/ticket/scenic/{scenicAreaId}")
    public Result<?> getTicketsByScenicAreaId(@PathVariable Integer scenicAreaId) {
        List<TicketDTO> tickets = ticketService.listTicketsByScenicAreaId(scenicAreaId);
        return Result.success(tickets);
    }

    /**
     * 根据ID获取门票详情
     */
    @GetMapping("/ticket/{id}")
    public Result<?> getTicketById(@PathVariable Integer id) {
        TicketDTO ticket = ticketService.getTicketById(id);
        return Result.success(ticket);
    }

    /**
     * 新增门票（管理员权限）
     */
    @PostMapping("/ticket/add")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> addTicket(@RequestBody Ticket ticket) {
        TicketDTO savedTicket = ticketService.addTicket(ticket);
        return Result.success("添加成功", savedTicket);
    }

    /**
     * 更新门票信息（管理员权限）
     */
    @PutMapping("/ticket/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> updateTicket(@PathVariable Integer id, @RequestBody Ticket ticket) {
        TicketDTO updatedTicket = ticketService.updateTicket(id, ticket);
        return Result.success("更新成功", updatedTicket);
    }
    
    /**
     * 仅更新门票数量（管理员权限）
     */
    @PutMapping("/ticket/quantity/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> updateTicketQuantity(@PathVariable Integer id, @RequestBody Map<String, Integer> quantityInfo) {
        Integer totalQuantity = quantityInfo.get("totalQuantity");
        Integer availableQuantity = quantityInfo.get("availableQuantity");
        
        if (totalQuantity == null || availableQuantity == null) {
            return Result.error("数量参数不完整");
        }
        
        if (availableQuantity > totalQuantity) {
            return Result.error("可用数量不能大于总数量");
        }
        
        TicketDTO updatedTicket = ticketService.updateTicketQuantity(id, totalQuantity, availableQuantity);
        return Result.success("数量更新成功", updatedTicket);
    }

    /**
     * 删除门票（管理员权限）
     */
    @DeleteMapping("/ticket/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> deleteTicket(@PathVariable Integer id) {
        ticketService.deleteTicket(id);
        return Result.success("删除成功");
    }
} 